<?php
/**
 * @Name: StaffLogic.php
 * @Author: yashuai
 */

namespace App\Http\Logic\Org;

use App\Http\Logic\BaseLogic;
use App\Http\Logic\Rbac\UserLogic;
use App\Http\Model\ConstCode;
use App\Http\Model\Org\StaffModel;
use App\Http\Model\Rbac\UserModel;

class StaffLogic extends BaseLogic
{

    /**
     * @Desc 添加员工
     * @return bool|int
     * @author yashuai
     */
    public function create()
    {
        return StaffModel::newInsert($this->getParamsTableData());
    }

    /**
     * @Desc 编辑员工
     * @return int
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function update()
    {
        $info = StaffModel::getInfo($this->params['StaffID'], 'StaffID');
        if (empty($info)) {
            _throw(106001);
        }
        $data = $this->getParamsTableData($info);
        return StaffModel::query()->where(['StaffID' => $this->params['StaffID']])->update($data);
    }

    /**
     * @Desc 删除员工
     * @return int
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function delete()
    {
        $info = StaffModel::getInfo($this->params['StaffID'], 'StaffID');
        if (empty($info)) {
            _throw(106001);
        }
        if ($info['Status'] == ConstCode::BASIC_STATUS_DELETE) {
            return 0;
        }
        return StaffModel::query()->where(['StaffID' => $this->params['StaffID']])->update([
            'Status' => ConstCode::BASIC_STATUS_DELETE,
        ]);
    }

    /**
     * @Desc 员工绑定登录账户（创建新的账户）
     * @tips 需要事务
     * @return bool
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function relationNewUser()
    {
        $staff = StaffModel::recordExist($this->params['StaffID'], 'StaffID', 'Status', ['UserID']);
        if (!empty($staff['UserID'])) {
            _throw(106002, '当前员工已绑定登录账户');
        }
        $userID = UserLogic::instance($this->params)->create();
        StaffModel::query()->where(['StaffID' => $this->params['StaffID']])->update(['UserID' => $userID]);
        return true;
    }

    /**
     * @Desc 员工绑定登录账户（关联已有账户）
     * @return bool
     * @throws \App\Exceptions\ApiException
     * @author yashuai
     */
    public function relationTableUser()
    {
        $staff = StaffModel::recordExist($this->params['StaffID'], 'StaffID', 'Status', ['UserID']);
        if (!empty($staff['UserID'])) {
            _throw(106002, '当前员工已绑定登录账户');
        }
        UserModel::recordExist($this->params['UserID'], 'UserID', 'UserStatus', '*', '当前账户不存在');
        $result = StaffModel::query()->where(['UserID' => $this->params['UserID']])->exists();
        if (!empty($result)) {
            _throw(106002, '当前账户已被其他员工绑定');
        }
        StaffModel::query()->where(['StaffID' => $this->params['StaffID']])->update([
            'UserID' => $this->params['UserID']
        ]);
        return true;
    }

    /**
     * @Desc 数据转为表结构数据
     * @return array
     * @author yashuai
     */
    private function getParamsTableData($info = [])
    {
        $defaultStatus = _array_field($info, 'Status', ConstCode::BASIC_STATUS_NORMAL);
        return [
            'DepartmentID' => $this->params['DepartmentID'],
            'StaffName'    => $this->params['StaffName'],
            'StaffOrder'   => $this->params['StaffOrder'],
            'Gender'       => $this->params['Gender'],
            'Mobile'       => _array_field($this->params, 'Mobile', _array_field($info, 'Mobile', '')),
            'Position'     => _array_field($this->params, 'Position', _array_field($info, 'Position', '')),
            'Email'        => _array_field($this->params, 'Email', _array_field($info, 'Email', '')),
            'Avatar'       => _array_field($this->params, 'Avatar', _array_field($info, 'Avatar', '')),
            'EnglishName'  => _array_field($this->params, 'EnglishName', _array_field($info, 'EnglishName', '')),
            'Status'       => _array_field($this->params, 'Status', $defaultStatus),
        ];
    }

}
